<?php

namespace Home\Controller;

/**
 *
 * @author Administrator
 *        
 */
class SFSController extends BaseController {
	/**
	 * 帐号查询
	 */
	public function mb_AccountQuery($name) {
		$userInfo = M ( 'Userinfo' );
		$userData = $userInfo->where ( "name='$name'" )->find ();
		if (isset ( $userData ) == false) {
			$this->mb_OutMessage ( ErrCode_Toast, '该帐号不存在' );
			return;
		}
		
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $userData ) );
	}
	/**
	 *
	 * @param unknown $id        	
	 */
	public function mb_AccountQueryForID($id) {
		$userInfo = M ( 'Userinfo' );
		$userData = $userInfo->where ( "id='$id'" )->find ();
		if (isset ( $userData ) == false) {
			$this->mb_OutMessage ( ErrCode_Toast, '该帐号不存在' );
			return;
		}
		
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $userData ) );
	}
	/**
	 * 帐号保存
	 */
	public function mb_AccountSave($name, $coin, $gameId, $remark) {
		$userInfo = M ( 'Userinfo' );
		$userData = $userInfo->where ( "name='$name'" )->find ();
		if (isset ( $userData ) == false) {
			$this->mb_OutMessage ( ErrCode_Toast, '该帐号不存在' );
			return;
		}
		$id = $userData ['id'];
		$bCoin = $userData ['coin'];
		$lCoin = $bCoin + $coin;
		
		if ($lCoin < 0) {
			$this->mb_OutMessage ( ErrCode_Toast, '该帐号金币不足' );
			return;
		}
		
		$lCoin = round ( $lCoin, 3 );
		$userDataLog = M ( 'Userdatalog' );
		
		$userInfo->save ( array (
				'id' => $id,
				'coin' => $lCoin 
		) );
		
		$userDataLog->add ( array (
				'name' => $name,
				'beforecoins' => $bCoin,
				'coins' => $coin,
				'latercoins' => $lCoin,
				'gameid' => $gameId,
				'remark' => $remark 
		) );
		$this->mb_OutMessage ( ErrCode_Normal, ( string ) $lCoin );
	}
	/**
	 * 通知大厅更新金币
	 *
	 * @param unknown $name        	
	 */
	public function mb_UpdateUserDataPush($name) {
		\WMHandler::mb_GetInstance ()->mb_UpdateUserData ( $name );
	}
	/**
	 * 玩家下注
	 *
	 * @param unknown $name        	
	 */
	public function mb_UserBet($name, $bet, $gameId, $remark) {
		$userInfo = M ( 'Userinfo' );
		$userData = $userInfo->where ( "name='$name'" )->find ();
		if (isset ( $userData ) == false) {
			$this->mb_OutMessage ( ErrCode_Toast, '该用户不存在' );
			return;
		}
		$code = $userData ['code'];
		$userBetLog = M ( 'Userbetlog' );
		$userBetLog->add ( array (
				'name' => $name,
				'code' => $code,
				'bets' => $bet,
				'gameid' => $gameId,
				'remark' => $remark 
		) );
		$this->mb_OutMessage ( ErrCode_Normal, ErrMsg_Normal );
	}
	/**
	 *
	 * @param unknown $name        	
	 * @param unknown $bets        	
	 * @param unknown $wins        	
	 */
	public function mb_UserBetsAndWins($name, $bet, $win, $gameId, $betRemark, $winRemark) {
		$userInfo = M ( 'Userinfo' );
		$userData = $userInfo->where ( "name='$name'" )->find ();
		if (isset ( $userData ) == false) {
			$this->mb_OutMessage ( ErrCode_Toast, '该用户不存在' );
			return;
		}
		$newBets = $userData ['bets'] + $bet;
		$newWins = $userData ['wins'] + $win;
		
		$userInfo->where ( "name='$name'" )->save ( array (
				'bets' => $newBets,
				'wins' => $newWins 
		) );
		
		$code = $userData ['code'];
		$userBetLog = M ( 'Userbetlog' );
		$userBetLog->add ( array (
				'name' => $name,
				'code' => $code,
				'bets' => $bet,
				'gameid' => $gameId,
				'remark' => $betRemark 
		) );
		$userWinLog = M ( 'Userwinlog' );
		$userWinLog->add ( array (
				'name' => $name,
				'code' => $code,
				'wins' => $win,
				'gameid' => $gameId,
				'remark' => $winRemark 
		) );
		$this->mb_OutMessage ( ErrCode_Normal, ErrMsg_Normal );
	}
	/**
	 *
	 * @param unknown $name        	
	 * @param unknown $win        	
	 * @param unknown $gameId        	
	 */
	public function mb_UserWin($name, $win, $gameId) {
		$userInfo = M ( 'Userinfo' );
		$userData = $userInfo->where ( "name='$name'" )->find ();
		if (isset ( $userData ) == false) {
			$this->mb_OutMessage ( ErrCode_Toast, '该用户不存在' );
			return;
		}
		$code = $userData ['code'];
		$userWinLog = M ( 'Userwinlog' );
		$userWinLog->add ( array (
				'name' => $name,
				'code' => $code,
				'wins' => $win,
				'gameid' => $gameId,
				'remark' => '' 
		) );
		$this->mb_OutMessage ( ErrCode_Normal, ErrMsg_Normal );
	}
	public function mb_UserDataLog($name, $before, $later, $gameId) {
	}
	/**
	 * 清除数据
	 */
	public function mb_DeleteData() {
		$adminName = C ( 'ADMIN_NAME' );
		$deveName = C ( 'DEVE_NAME' );
		$manaName = C ( 'MANA_NAME' );
		
		$adminInfo = M ( 'Admininfo' );
		$adminInfo->where ( "name!='$adminName' AND name!='$deveName' AND name!='$manaName'" )->delete ();
		
		$adminProfitLog = M ( 'Adminprofitlog' );
		$adminProfitLog->where ( '1' )->delete ();
		
		$adminRechargeLog = M ( 'Adminrechargelog' );
		$adminRechargeLog->where ( '1' )->delete ();
		
		$applyCashLog = M ( 'Applycashlog' );
		$applyCashLog->where ( '1' )->delete ();
		
		$clockInLog = M ( 'Clockinlog' );
		$clockInLog->where ( '1' )->delete ();
		
		$userBetLog = M ( 'Userbetlog' );
		$userBetLog->where ( '1' )->delete ();
		
		$userDataLog = M ( 'Userdatalog' );
		$userDataLog->where ( '1' )->delete ();
		
		$userInfo = M ( 'Userinfo' );
		$userInfo->where ( '1' )->delete ();
		
		$userRechargeLog = M ( 'Userrechargelog' );
		$userRechargeLog->where ( '1' )->delete ();
		
		$userWinLog = M ( 'Userwinlog' );
		$userWinLog->where ( '1' )->delete ();
		$this->mb_OutMessage ( ErrCode_Normal, ErrMsg_Normal );
	}
}